package com.siburian.MyStuff;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



public class DBStudyType {
	private static final String ROW_ID = "ID_Pelajaran";
	private static final String ID_KELAS = "ID_Kelas";
	private static final String NAMA_PELAJARAN = "Nama_Pelajaran";
	private static final String JUMLAH_BAB = "JumlahBab";
	 
	private static final String NAMA_DB = "DBStudyType";// nama database
	private static final String NAMA_TABEL = "Study";// nama tabel
	private static final int DB_VERSION = 1;
 
	private static final String CREATE_TABLE_STUDYTYPE = "create table "+NAMA_TABEL+" ("+ROW_ID+" integer PRIMARY KEY autoincrement, "+ID_KELAS+" integer,"+NAMA_PELAJARAN+" text,"+JUMLAH_BAB+" integer)";
	private final Context context;
	private DatabaseOpenHelper dbHelper;
	private SQLiteDatabase db;
 
	public DBStudyType(Context ctx) {
		this.context = ctx;
		dbHelper = new DatabaseOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}
 
	private static class DatabaseOpenHelper extends SQLiteOpenHelper {
		 
		public DatabaseOpenHelper(Context context) {
			super(context, NAMA_DB, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_TABLE_STUDYTYPE);
		}
 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+NAMA_TABEL);
			onCreate(db);
 
		}
	}
 
	public void close() {
		dbHelper.close();
	}
 
	public void addProfile(int idKelas,String nama_pelajaran,int jlhBab) {
 
		ContentValues values = new ContentValues();
		values.put(ID_KELAS, idKelas);
		values.put(NAMA_PELAJARAN, nama_pelajaran); 
		values.put(JUMLAH_BAB, jlhBab);
		try {
 
		//	db.delete(NAMA_TABEL, null, null);
			db.insert(NAMA_TABEL, null, values);
		} catch (Exception e) {
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}
	}
	
	
	public ArrayList<ArrayList<Object>> getProfile(){
		ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { ROW_ID,ID_KELAS, NAMA_PELAJARAN,JUMLAH_BAB }, null, null,
					null, null, null);
			cur.moveToFirst();
			if (!cur.isAfterLast()) {
				do {
					ArrayList<Object> dataList = new ArrayList<Object>();
					dataList.add(cur.getLong(0));
					dataList.add(cur.getString(1));
					dataList.add(cur.getString(2));
					dataList.add(cur.getString(3));
					
 
					dataArray.add(dataList);
 
				} while (cur.moveToNext());
 
			}
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return dataArray;
	}
	public void generateData(){
		ContentValues values = new ContentValues();
		values.put(ID_KELAS, 1);
		values.put(NAMA_PELAJARAN, "Membaca"); 
		values.put(JUMLAH_BAB, 12);
		db.insert(NAMA_TABEL, null, values);
		values.put(ID_KELAS, 1);
		values.put(NAMA_PELAJARAN, "Berhitung"); 
		values.put(JUMLAH_BAB, 12);
		db.insert(NAMA_TABEL, null, values);

	}
	public int getBabUsingStudyType(int idKelas){
		int i = 0;
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { ROW_ID,ID_KELAS, NAMA_PELAJARAN,JUMLAH_BAB },  ID_KELAS + "=" + '"' + idKelas + '"', null,
					null, null, null);
			cur.moveToFirst();
			if (!cur.isAfterLast()) {
				do {
					ArrayList<Object> dataList = new ArrayList<Object>();
					i = cur.getInt(3);
				} while (cur.moveToNext());
			}
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return i;
	}

}
